package com.lyy.service.impl;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;

/**
 * @author Liangyaoyun
 */
@Service
@CacheConfig(cacheNames = "dog")
public class DogServiceImpl {
    @AllArgsConstructor
    @Data
    static class Dog implements Serializable{
        public Integer dogId;
        public String dogName;
    }
    @CachePut(key ="#result.dogId")
    public Dog insert(Dog dog)
    {
        System.out.println("=> insert into 狗表 () values ()");
        //返回刚插入的那条狗
        return new Dog(1,"小白");
    }
    @Cacheable(key ="#dogId")
    public Dog select(Integer dogId) {
        System.out.println("=> select * from 狗表 where dog id = " + dogId);
        //返回查询到的那条狗
        return new Dog(1,  "小白");
    }
    @CacheEvict(key ="#dogId")
    public Dog update(Integer dogId, Dog condition) {
        System.out.println("=> update 狗表 set .. where dog id = " + dogId);
        //返回修改后的那条狗
        return new Dog(1, "小黑");
    }
    @CacheEvict(key ="#dogId")
    public Dog delete(Integer dogId)
    {
        System.out.println("=> delete from 狗表 where dog id = " + dogId);
        //返回被删除的那条狗
        return new Dog(1,"小白");
    }
}
